import Router from 'koa-router'
import fs from 'fs'
import {myTools} from "../../assets/myTools/jsTools/jsTools"
// import { myTools } from '../../assets/myTools/jsTools/jsTools'
// import User from '../dbs/module/user'

const router = new Router({ prefix: '/usr' })

router.post('/getUsrData', async (ctx) => {
  let { params: { uid }} = ctx.request.body

  let usrsData = fs.readFileSync('static/usr.json', 'utf-8')
  let usrsObj = JSON.parse(usrsData)
  let usr = usrsObj[uid]

  ctx.body = {
     code: 0,
     data: usr
  }
})

router.post('/getUsrCart', async (ctx) => {
  let { params: { uid }} = ctx.request.body

  let usrsData = fs.readFileSync('static/usr.json', 'utf-8')
  let usrsObj = JSON.parse(usrsData)
  let usr = usrsObj[uid]

  ctx.body = {
    code: 0,
    data: usr.cart
  }
})

router.post('/getUsrBill', async (ctx) => {
  let { params: { uid }} = ctx.request.body

  let usrsData = fs.readFileSync('static/usr.json', 'utf-8')
  let usrsObj = JSON.parse(usrsData)
  let usr = usrsObj[uid]

  ctx.body = {
    code: 0,
    data: usr.bills
  }
})

router.post('/setUsrCartCount', async (ctx) => {
  let { params: { uid, skuid, count }} = ctx.request.body

  let usrsData = fs.readFileSync('static/usr.json', 'utf-8')
  let usrsObj = JSON.parse(usrsData)
  let usr = usrsObj[uid]
  usr.cart.forEach(i=>{
    if (i.sku_id === skuid){
      i.itemCount = count
    }
  })
  fs.writeFileSync('static/usr.json', JSON.stringify(usrsObj), 'utf-8')
  ctx.body = {
    code: 0
  }
})

router.post('/deleteBill', async (ctx) => {
  let { params: { uid, bill_id }} = ctx.request.body

  let usrsData = fs.readFileSync('static/usr.json', 'utf-8')
  let usrsObj = JSON.parse(usrsData)
  let usr = usrsObj[uid]
  let idx
  usr.bills.some((i,n)=>{
    idx = n
    return i.bill_id === bill_id
  })
  usr.bills.splice(idx, 1)
  fs.writeFileSync('static/usr.json', JSON.stringify(usrsObj), 'utf-8')
  ctx.body = {
    code: 0,
    data: usr.bills
  }
})

router.post('/addCart', async (ctx) => {
  let { params: { uid, skuid, count }} = ctx.request.body

  let usrsData = fs.readFileSync('static/usr.json', 'utf-8')
  let usrsObj = JSON.parse(usrsData)
  let usr = usrsObj[uid]
  let idx = myTools.arrSomeItem(usr.cart, 'sku_id', skuid)
  if(idx === false){
    usr.cart.push({
      sku_id: skuid,
      itemCount: count
    })
    fs.writeFileSync('static/usr.json', JSON.stringify(usrsObj), 'utf-8')
    ctx.body = {
      code: 0,
      msg: '已添加到购物车',
      data: 'succeed'
    }
  }else{
    ctx.body = {
      code: 0,
      msg: '已添加到购物车',
      data: 'succeed'
    }
  }
})

router.post('/addBill', async (ctx) => {
  let {params: {uid, bill, from}} = ctx.request.body

  let usrsData = fs.readFileSync('static/usr.json', 'utf-8')
  let usrsObj = JSON.parse(usrsData)
  let usr = usrsObj[uid]

  if(from === 'product') {
    usr.bills.push(bill)
    fs.writeFileSync('static/usr.json', JSON.stringify(usrsObj), 'utf-8')
    ctx.body = {
      code: 0,
      data: 'succeed'
    }
  }else if(from === 'cart') {
    usr.bills.push(bill)
    usr.cart = []
    fs.writeFileSync('static/usr.json', JSON.stringify(usrsObj), 'utf-8')
    ctx.body = {
      code: 0,
      data: 'succeed'
    }
  }else{
    console.log('sever bill error')
    ctx.body = {
      code: 0,
      data: 'succeed'
    }
  }
})

export default router
